package com.example.suze.libraryinformationmanagementsystem.Activity;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Base64;
import android.util.Log;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.suze.libraryinformationmanagementsystem.Adapter.BookListAdapter;
import com.example.suze.libraryinformationmanagementsystem.Adapter.PersonListAdapter;
import com.example.suze.libraryinformationmanagementsystem.R;
import com.example.suze.libraryinformationmanagementsystem.net.API;
import com.example.suze.libraryinformationmanagementsystem.net.JSONCallBack;
import com.example.suze.libraryinformationmanagementsystem.net.entiry.BookRoot;
import com.example.suze.libraryinformationmanagementsystem.net.entiry.InfoDatum;
import com.example.suze.libraryinformationmanagementsystem.net.entiry.InfoRoot;
import com.person.dao.BookDao;
import com.person.dao.DBManager;
import com.person.dao.NotesDao;
import com.person.dao.PersonDao;
import com.person.dao.StudentDao;
import com.person.entity.Book;
import com.person.entity.Notes;
import com.person.entity.Person;
import com.person.entity.Student;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by suze on 2017/6/12.
 */

public class PersonActivity extends BaseActivity{
    private ListView listView;
    private TextView name,id2,school,profession,class1;
    private ImageView head;
    private String name1,id1,school1,profession1,class2,img1;
    private JSONCallBack callBack;
    private API api = API.getAPI(this);
    private String person;
    private List<InfoDatum> list = new ArrayList<>();
    private final int UPDATE_ACCOUNT = 2;


    //适配;线程
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == UPDATE_ACCOUNT) {
                int arg = msg.arg1;
                if (arg == 1) {
                    PersonListAdapter personListAdapter = new PersonListAdapter(PersonActivity.this,list);
                    listView.setAdapter(personListAdapter);
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person);
        listView = (ListView) findViewById(R.id.list_person);
        name = (TextView) findViewById(R.id.tv_person_name);
        id2 = (TextView) findViewById(R.id.tv_person_id);
        school = (TextView) findViewById(R.id.tv_person_school);
        profession = (TextView) findViewById(R.id.tv_person_profession);
        class1 = (TextView) findViewById(R.id.tv_person_class);
        head = (ImageView) findViewById(R.id.iv_person);
        Bundle bundle = getIntent().getExtras();
        person = bundle.getString( "borrowerId");
//        DBManager dbManager = new DBManager(this);
//        name1 = list.get(0).getName();
//        school1 = list.get(0).getSchool();
//        class2 = list.get(0).getClass1();
//        profession1 = list.get(0).getProfession();
//        img1 = list.get(0).getImg();
        name.setText("姓名：吴彦祖");
        id2.setText("学号：5120145303");
        school.setText("学院：计算机科学与技术学院");
        profession.setText("专业：信息安全");
        class1.setText("班级：信安1401");
        Resources res = getResources();
        head.setImageBitmap(BitmapFactory.decodeResource(res,R.drawable.wuyanzu));
        net();
//        NotesDao notesDao = dbManager.getDaoSession().getNotesDao();
//        List<Notes> notes = notesDao.queryBuilder().where(NotesDao.Properties.PersonId.eq(person)).build().list();
//        if(notes!=null && notes.size()>0){
//            PersonListAdapter personListAdapter = new PersonListAdapter(this,notes);
//            listView.setAdapter(personListAdapter);
//        }
    }
    /**
     * base64转bitmap
     *
     * @param base64String
     * @return
     */
    public static Bitmap base64ToBitmap(String base64String) {
        byte[] bytes = Base64.decode(base64String, Base64.DEFAULT);
        Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
        return bitmap;
    }
    private void net(){
        callBack = new JSONCallBack<InfoRoot>(new InfoRoot()) {
            @Override
            public void onSuccess(InfoRoot entity) {
                if(entity.getstate().equals("success") && entity.getcode() == 0){
                    if(entity.getdata().size()>0){
                        list.clear();
                        for(int i =0;i<entity.getdata().size();i++)
                        {
                            list.add(entity.getdata().get(i));
                        }
                        Thread thread = new Thread(new Runnable() {
                            @Override
                            public void run() {
                                Message message = new Message();
                                message.arg1 = 1;
                                message.what = UPDATE_ACCOUNT;
                                handler.sendMessage(message);
                            }
                        });
                        thread.start();
                    }
                }else{
                    Looper.prepare();
                    Toast.makeText(PersonActivity.this, "请求数据失败", Toast.LENGTH_LONG).show();
                    Looper.loop();
                }
            }
        };
        api.borrowInfo("select", "SELECT * FROM borrow_information WHERE  reader_name = '"+person+"'", callBack);
    }
}
